package com.chenwut.myreflect.a05_reflect_exercise_2;

import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

/**
 * @author ChenPi
 * @since 2024/09/24 09:02
 */
public class MyReflectDemo {
    public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        /**
         * 反射可以跟配置文件结合的方式，动态的创建对象，并调用方法
         */

        Properties prop = new Properties();
        FileInputStream fis = new FileInputStream(".\\SE_Code\\src\\com\\chenwut\\myreflect\\prop.properties");
        prop.load(fis);
        fis.close();
        System.out.println(prop);

        String className = prop.getProperty("className");
        String methodName = prop.getProperty("method");

        Class clazz = Class.forName(className);
        Constructor con = clazz.getDeclaredConstructor();
        con.setAccessible(true);

        Object instance = con.newInstance();
        System.out.println(instance);


        Method method = clazz.getDeclaredMethod(methodName);

        method.setAccessible(true);
        method.invoke(instance);

    }
}
